package com.dao;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import com.entity.Goods;
import com.entity.Shop;

@Repository("goodsMapper")
public interface GoodsDao {
	@Select("select * from goods where issale=1 order by type desc")
	List<Goods> selectAllGoods();
	
	@Select("select *\r\n"
			+ "from goods t where t.issale=1\r\n"
			+ "order by case when t.type = #{type} THEN 1 else 2 end")
	List<Goods> selectUniqueGoods(String type);

	@Select("select * from goods where good_id=#{good_id}")
	Goods selectGoodById(Integer good_id);

	@Select("select * from shop where shop.shop_id=(select shop_id from goods where good_id=#{good_id})")
	Shop selectShopByGoodId(Integer good_id);

	@Select("select * from goods where shop_id=#{shop_id} order by type desc")
	List<Goods> selectAllGoodsByShopId(Integer shop_id);

	@Select("select * from goods where type=#{type}")
	List<Goods> selectAllGoodsByType(String type);

	@Update("update goods set issale=0 where good_id=#{good_id}")
	int updateIsSale(Integer good_id);

	@Insert("insert into goods values (default,#{good_name},#{shop_id},#{g_image},#{number},#{price},0,#{good_size},1,#{good_desc},#{type})")
	int insertFile(Goods goods);

	
	@Delete("delete from goods where good_id=#{good_id}") 
	int delete(Integer good_id);
	 
	@Update("update goods set good_name=#{good_name},g_image=#{g_image},number=#{number},price=#{price},good_size=#{good_size},good_desc=#{good_desc},type=#{type} where good_id=#{good_id}") 
	int update(Goods goods);
	  
	@Update("update goods set buy_times=buy_times+#{buyNum} ,number=number-#{goodsNum} where good_id=#{good_id}") 
	int updatebuy(@Param("good_id")Integer good_id,@Param("buyNum")Integer buyNum,@Param("goodsNum")Integer goodsNum);
	
	@Update("update goods set number=number-#{goodsNum} where good_id=#{good_id}") 
	int updatebefore(@Param("good_id")Integer good_id,@Param("goodsNum")Integer goodsNum);
	
	@Select("select * from goods where shop_id=#{shop_id} and issale=1")
	List<Goods> selectAllGoodssale(Integer shop_id);
	
	@Select("select * from goods where shop_id=#{shop_id} and issale=0")
	List<Goods> selectAllGoodsnosale(Integer shop_id);
	
	@Select("select * from goods where good_name like concat('%',#{good_name},'%') order by buy_times")
	List<Goods> selectGoodsByName(@Param("good_name") String selector);
	 
	@Update("update goods set issale=0 where good_id=#{good_id}")
	int updateGoodState(@Param("good_id") Integer goodid);
}
